﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using CE.Board;

public partial class board_answer_question : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[CE.Global.General.BoardId] == null)
            {
                Response.Redirect("../board/login.aspx");
            }
            else
            {
                liLogout.Visible = true;
            }

            GetResult();
        }
        else
        {
            lblMessage.Text = "";
            lblSubmitMessage.Text = "";
        }
    }

    #region Result

    private void GetResult()
    {
        try
        {
            grdResult.DataSource = null;
            grdResult.DataBind();

            BoardDB objBoardDB = new BoardDB();
            DataTable _dt = objBoardDB.GetBoard(txtSearch.Text.Trim(), int.Parse(ddlPublish.SelectedValue), txtSearchName.Text.Trim(), txtSearchPhone.Text.Trim(), txtSearchEmail.Text.Trim());

            grdResult.DataSource = _dt;
            grdResult.DataBind();
        }
        catch (Exception)
        {
        }
    }

    protected void grdResult_ItemCommand(object sender, DataGridCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.ToLower().Equals("editboard"))
            {
                HiddenField hidId = (HiddenField)e.Item.FindControl("hidBoardId");
                hidBId.Value = hidId.Value;
                Board objBoard = new Board();
                BoardDB objBoardDb = new BoardDB();
                objBoard = objBoardDb.GetBoardSingleItem(int.Parse(hidBId.Value));
                txtSubject.Text = objBoard.Subject;
                txtQuestion.Text = objBoard.Question;
                //string rplvalue = objBoard.Answer.Replace("\r\n", "<br>");
                //rplvalue = rplvalue.Replace("\n", "<br>");
                txtAnswer.Text = objBoard.Answer;
                chkPublish.Checked = objBoard.IsPublished;
                lblAskedBy.Text = objBoard.Name + "  <b>Email:</b>  " + objBoard.Email + "  <b>Phone#:</b>  " + objBoard.Phone;

                mpeAnswer.Show();
            }
            else if (e.CommandName.ToLower().Equals("deleteboard"))
            {
                HiddenField hidId = (HiddenField)e.Item.FindControl("hidBoardId");
                if (hidId.Value != "")
                {
                    BoardDB objBoardDB = new BoardDB();
                    Int64 rtnVal = objBoardDB.DeleteBoard(Int64.Parse(hidId.Value));
                    if (rtnVal >= 0)
                    {
                        grdResult.CurrentPageIndex = 0;
                        GetResult();
                    }
                    else
                    {
                        throw new Exception("");
                    }
                }
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = "Error found! Please try it later again.<br/>" + Ex.Message;
            lblMessage.Font.Bold = true;
        }
    }

    protected void grdResult_Change(Object sender, DataGridPageChangedEventArgs e)
    {
        grdResult.CurrentPageIndex = e.NewPageIndex;
        GetResult();
    }

    //public string ReplaceString(string txtvalue)
    //{
    //    string rplvalue = txtvalue.Replace("\r\n", "<br>");
    //    rplvalue = rplvalue.Replace("\n", "<br>");
    //    return rplvalue;
    //}

    #endregion

    protected void btnSubmitForm_Click(object sender, EventArgs e)
    {
        try
        {
            if (hidBId.Value.Trim() != "")
            {
                Board objBoard = new Board();
                BoardDB objBoardDb = new BoardDB();
                objBoard = objBoardDb.GetBoardSingleItem(int.Parse(hidBId.Value));

                objBoard.Subject = txtSubject.Text;
                objBoard.Question = txtQuestion.Text;
                objBoard.Answer = txtAnswer.Text;
                objBoard.IsPublished = chkPublish.Checked;
                objBoard.AnswerBy = Convert.ToInt32(Session[CE.Global.General.BoardId]);
                objBoard.AnswerDate = DateTime.Now;
                if (objBoardDb.Insert(objBoard) > 0)
                {
                    GetResult();
                }
                else
                {
                    throw new Exception("");
                }
            }
            mpeAnswer.Hide();
        }
        catch (Exception Ex)
        {
            lblSubmitMessage.Text = "Error found! Please try it later again.<br/>" + Ex.Message;
            lblSubmitMessage.Font.Bold = true;
        }
    }

    protected void btnSearch_Click(object sender, ImageClickEventArgs e)
    {
        grdResult.CurrentPageIndex = 0;
        GetResult();
    }
}
